﻿#region License Info
//Component of Cronos Package, http://www.codeplex.com/cronos
//Copyright (C) 2009 Anthony Brockwell

//This program is free software; you can redistribute it and/or
//modify it under the terms of the GNU General Public License
//as published by the Free Software Foundation; either version 2
//of the License, or (at your option) any later version.

//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with this program; if not, write to the Free Software
//Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#endregion


using System;
using System.Collections.Generic;
using ABMath.ModelFramework.Data;
using MathNet.Numerics.LinearAlgebra;

namespace ABMath.ModelFramework.Visuals
{
    [Serializable]
    public class CrossCorrelationVisual : Visual
    {
        private const int maxLag = 35;

        private MVTimeSeries mvts;

        [NonSerialized]
        public Matrix[] ACF;

        public int TSCount
        { 
            get
            {
                if (mvts == null)
                    return 0;
                if (mvts.Dimension < 2)
                    return 0;  // doesn't make sense for univariate TS
                return mvts.Count;
            } 
        }

        public override string GetDescription()
        {
            return "Computes cross-correlation functions of a multivariate input";
        }

        public override string GetShortDescription()
        {
            return "Cross-Corr.";
        }

        public string SubTitle(int index)
        {
            if (mvts.SubTitle != null)
                if (mvts.SubTitle[index] != null)
                    return mvts.SubTitle[index];
            return "Comp #" + (index + 1);
        }

        protected override bool OnInputChanged()
        {
            mvts = TheObject as MVTimeSeries;
            if (mvts == null)
                return false;
            if (mvts.Dimension < 2)
                return false;  // doesn't make sense for univariate TS

            ACF = mvts.ComputeACF(maxLag, true);

            if (AssociatedControl != null)
                AssociatedControl.DataHasChanged();

            return true;
        }

        public override List<Type> GetAllowedInputTypesFor(int socket)
        {
            if (socket != 0)
                throw new SocketException();
            return new List<Type> { typeof(MVTimeSeries) };
        }
    }
}
